package net.spacebeeshooter.main;

import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import net.spacebeeshooter.gui.AddListener;
import net.spacebeeshooter.gui.ButtonPanel;
import net.spacebeeshooter.gui.DeleteListener;
import net.spacebeeshooter.gui.EditDefaultListener;
import net.spacebeeshooter.gui.EditorPanel;
import net.spacebeeshooter.gui.Frame;
import net.spacebeeshooter.gui.MainPanel;
import net.spacebeeshooter.gui.MenuBar;
import net.spacebeeshooter.gui.MenuButton;
import net.spacebeeshooter.gui.SaveListener;
import net.spacebeeshooter.gui.TabPanel;
import net.spacebeeshooter.gui.TableSaver;
import net.spacebeeshooter.items.Item;

public class GUIConfigurator {
	private static final String SAVE_SEPARATOR = String.valueOf((char)9);
	private static final String SAVE_DESCRIPTION = "Save";
	private static final String EXIT_DESCRIPTION = "Exit";
	private static final String ADD_TAB_LOCATION = "res/add.png";
	private static final String ADD_TAB_ERROR_TEXT = "Add new tab";
	private static final String DELETE_TAB_LOCATION = "res/delete.png";
	private static final String DELETE_TAB_ERROR_TEXT = "Close current tab";
	private static final String EDIT_DEFAULT_LOCATION = "res/edit_default.png";
	private static final String EDIT_DEFAULT_ERROR_TEXT = "Edit item default";
	private static final String TITLE = "Space Bee Shooter Item Generator";
	private static final String LOGO_LOCATION = "res/sbs_logo.png";
	
	public JFrame buildGui(Item defaultItem, String defaultLocation) {
		
		EditorPanel editorPanel = new EditorPanel(defaultItem);
		TabPanel tabPanel = new TabPanel(editorPanel);
		TableSaver saver = new TableSaver(defaultItem, SAVE_SEPARATOR);
		SaveListener saveListener = new SaveListener(saver, tabPanel);
		ButtonPanel buttonPanel = new ButtonPanel(saveListener, SAVE_DESCRIPTION, EXIT_DESCRIPTION);
		MainPanel mainPanel = new MainPanel(tabPanel, buttonPanel);
		ArrayList<Component> menuBarButtons = new ArrayList<Component>();
		MenuButton addTab = new MenuButton(ADD_TAB_LOCATION, ADD_TAB_ERROR_TEXT);
		addTab.addActionListener(new AddListener(tabPanel, defaultItem));
		menuBarButtons.add(addTab);
		MenuButton delTab = new MenuButton(DELETE_TAB_LOCATION, DELETE_TAB_ERROR_TEXT);
		delTab.addActionListener(new DeleteListener(tabPanel));
		menuBarButtons.add(delTab);
		MenuButton editDefault = new MenuButton(EDIT_DEFAULT_LOCATION, EDIT_DEFAULT_ERROR_TEXT);
		editDefault.addActionListener(new EditDefaultListener(defaultLocation));
		menuBarButtons.add(editDefault);
		MenuBar menuBar = new MenuBar(menuBarButtons);
		Frame frame = new Frame(mainPanel,menuBar);
		frame.setTitle(TITLE);
		String logoLoc = LOGO_LOCATION;
		try {
			frame.setIconImage(ImageIO.read(new File(logoLoc)));
		} catch(IOException e) {
			new ErrorGenerator().generateErrorMessage("Logo '" + logoLoc + "' could not be found!");
		}
		return frame;
	}
}
